package com.example.clinic.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.clinic.common.QueryPageParam;
import com.wyl.clinic.common.Result;
import com.example.clinic.entity.Dep;
import com.example.clinic.service.DepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-01-29
 */
@RestController
@RequestMapping("/dep")
public class DepController {
    @Autowired
    private DepService depService;


    //新增
    @PostMapping("/save")
    public Result save (@RequestBody Dep dep){
        return depService.save(dep)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Dep dep){
        return depService.updateById(dep)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return depService.removeById(id)?Result.suc():Result.fail();
    }
    //
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        System.out.println(param);
        String name = (String)param.get("name");
//        String roleId = (String)param.get("roleId");

        Page<Dep> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Dep> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Dep::getName,name);
        }



        IPage result = depService.page(page,lambdaQueryWrapper); //注意这里

        System.out.println("总记录"+result.getTotal());
        System.out.println(result.getRecords());


        return Result.suc(result.getRecords(),result.getTotal());
    }

}
